package manage.commons.cookie;

import javax.servlet.http.HttpServletRequest;

import manage.commons.utils.CookieUtils;

public class DefaultSessionManager implements SessionManager {

	private SessionHandler sessionHandler;

	public DefaultSessionManager() {
		sessionHandler = new SessionHandler();
	}

	public DefaultSessionManager(String cookieValue) {
		sessionHandler = new SessionHandler(cookieValue);
	}

	public DefaultSessionManager(HttpServletRequest request, String cookieKey) {
		String cookieValue = CookieUtils.getCookie(request, cookieKey);
		sessionHandler = new SessionHandler(cookieValue);
	}

	@Override
	public SessionHandler getSession() {
		return sessionHandler;
	}

	@Override
	public String getSessionString() {
		return sessionHandler.toString();
	}

	@Override
	public String getAttribute(String cookieKey) {
		return sessionHandler.get(cookieKey);
	}

	@Override
	public void setAttribute(String cookieKey, String cookieValue) {
		sessionHandler.put(cookieKey, cookieValue);
	}

	@Override
	public void setAttribute(String cookieKey, Object cookieValue) {
		sessionHandler.put(cookieKey, cookieValue);
	}

	@Override
	public void removeAttribute(String cookieKey) {
		sessionHandler.remove(cookieKey);
	}

	@Override
	public boolean containsKey(String cookieKey) {
		return sessionHandler.containsKey(cookieKey);
	}

	@Override
	public void invalidate() {
		if (null != sessionHandler) {
			sessionHandler = null;
		}
	}

}
